package com.zjun.demo.experimentaldemo.wifi_list

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.base.BaseActivity
import com.zjun.demo.experimentaldemo.databinding.ActivityLogTestBinding


/**
 * 获取wifi列表
 *
 * 情况①targetSdkVersion>=Build.VERSION_CODES.Q不再允许App开启或者关闭wifi
 * 位置权限,用于扫描wifi
 * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 *
 * 情况②targetSdkVersion<Build.VERSION_CODES.Q时,需要以下权限
 * 变更wifi权限，用于开启或者关闭wifi
 * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 * 位置权限,用于扫描wifi
 * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 *
 */
class WifiListActivity : BaseActivity<ActivityLogTestBinding>() {

    companion object{

        const val TAG = "WifiListActivity"

    }

    private lateinit var wifiManager: WifiManager

    override fun getLayoutId(): Int = R.layout.activity_log_test

    override fun initialize() {
        wifiManager = getSystemService(WIFI_SERVICE) as WifiManager
//        wifiManager.enableNetwork(netId, true);
        val packageInfo = packageManager.getPackageInfo(packageName, 0)
        if (packageInfo.applicationInfo.targetSdkVersion < Build.VERSION_CODES.Q && !wifiManager.isWifiEnabled) {
            if (!wifiManager.isWifiEnabled) {

                //修改系统wifi开关需要<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />权限并且targetSdkVersion<29
                wifiManager.setWifiEnabled(true)
            }
        }
        scanWifi()
    }

    private fun scanWifi(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            //wifiManager.getScanResults() 需要以下权限
            //<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
            //<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            val scanResults = wifiManager.scanResults
            for (result in scanResults) {
                val ssid = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    result.wifiSsid?.toString()?:""
                } else {
                    result.SSID
                }
                val bssid = result.BSSID
                val signalStrength = result.level
                Log.i(TAG, "scanWifi: ssid = $ssid,bssid = $bssid,signalStrength = $signalStrength")
            }
        }else{
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
                val dialog: AlertDialog = AlertDialog.Builder(this@WifiListActivity)
                    .setTitle("开启wifi") //设置对话框的标题
                    .setMessage("前往系统设置页面打开wifi") //设置对话框的内容
                    //设置对话框的按钮
                    .setNegativeButton("取消") { dialog, _ ->
                        Toast.makeText(this@WifiListActivity, "权限取消申请", Toast.LENGTH_SHORT).show()
                        dialog.dismiss()
                    }
                    .setPositiveButton("确定") { dialog, _ ->
                        toSystemWifiSettingsPage()
                        dialog.dismiss()
                    }.create()
                dialog.show()
            }else{
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),101)
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when(requestCode){

            101->{
                scanWifi()
            }

        }
    }

    /**
     * 手动跳转到wifi设置页面,让用户手动打开wifi
     */
    fun toSystemWifiSettingsPage(){
        val intent = Intent()
        intent.action = Settings.ACTION_WIFI_SETTINGS
        startActivity(intent)
    }

}